package com.zone.gateway.core.socket.agreement.impl;

import com.alibaba.fastjson.JSON;
import com.zone.gateway.core.socket.agreement.IRequestParser;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.CharsetUtil;

import java.util.Map;

/**
 * @author: zongzi
 * @description: content-type = json的post请求解析器
 * @date: 2025/4/22
 */
public class PostJsonParser implements IRequestParser {

    @Override
    public Map<String, Object> analysis(FullHttpRequest httpRequest) {
        // 1. 获取content
        ByteBuf content = httpRequest.content();
        String jsonBody = content.toString(CharsetUtil.UTF_8);

        // 2. 解析JSON到Map
        try {
            // 使用FastJSON解析
            return JSON.parseObject(jsonBody);
        } catch (Exception e) {
            throw new RuntimeException("Invalid JSON format");
        }
    }
}
